package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dao.PmsProductAttributeCategoryDao;
import com.macro.mall.dto.PmsProductAttributeCategoryItem;
import com.macro.mall.dto.UmsAdminDTO;
import com.macro.mall.mapper.PmsProductAttributeCategoryMapper;
import com.macro.mall.mapper.PmsProductMapper;
import com.macro.mall.model.PmsProduct;
import com.macro.mall.model.PmsProductAttributeCategory;
import com.macro.mall.model.PmsProductAttributeCategoryExample;
import com.macro.mall.model.PmsProductExample;
import com.macro.mall.service.PmsProductAttributeCategoryService;
import com.macro.mall.service.UmsAdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * PmsProductAttributeCategoryService实现类
 */
@Service
public class PmsProductAttributeCategoryServiceImpl implements PmsProductAttributeCategoryService {
    @Autowired
    private PmsProductAttributeCategoryMapper productAttributeCategoryMapper;
    @Autowired
    private PmsProductAttributeCategoryDao productAttributeCategoryDao;
    @Autowired
    private UmsAdminService umsAdminService;
    @Autowired
    private PmsProductMapper productMapper;

    @Override
    public int create(String name ,String currentUserName) {
        PmsProductAttributeCategory productAttributeCategory = new PmsProductAttributeCategory();
        productAttributeCategory.setName(name);
        UmsAdminDTO admin = this.umsAdminService.getAdminDTOByUserName(currentUserName);
        productAttributeCategory.setMemberId(admin.getId());
        productAttributeCategory.setMemberUserName(admin.getUserName());
        productAttributeCategory.setStoreId(admin.getStoreId());
        return productAttributeCategoryMapper.insertSelective(productAttributeCategory);
    }

    @Override
    public int update(PmsProductAttributeCategory attributeCategory,String currentUserName) {
        UmsAdminDTO admin = this.umsAdminService.getAdminDTOByUserName(currentUserName);
        PmsProductAttributeCategory productAttributeCategory =this.productAttributeCategoryMapper.selectByPrimaryKey(attributeCategory.getId());
        if(productAttributeCategory.getMemberId().equals(admin.getId())){
            return productAttributeCategoryMapper.updateByPrimaryKeySelective(attributeCategory);
        }else{
            return -2;
        }
    }

    @Override
    public int delete(Long id, String currentUserName) {
        UmsAdminDTO admin = this.umsAdminService.getAdminDTOByUserName(currentUserName);
        PmsProductExample example = new PmsProductExample();
        example.createCriteria().andProductAttributeCategoryIdEqualTo(id).andStoreIdEqualTo(admin.getStoreId());
        List<PmsProduct> pmsProductList = productMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(pmsProductList)) {
            return -2;
        }
        return productAttributeCategoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PmsProductAttributeCategory getItem(Long id) {
        return productAttributeCategoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<PmsProductAttributeCategory> getList(String currentUserName, Integer pageSize, Integer pageNum) {
        UmsAdminDTO admin = this.umsAdminService.getAdminDTOByUserName(currentUserName);
        PageHelper.startPage(pageNum,pageSize);
        PmsProductAttributeCategoryExample pmsProductAttributeCategoryExample = new PmsProductAttributeCategoryExample();
        pmsProductAttributeCategoryExample.createCriteria().andStoreIdEqualTo(admin.getStoreId());
        pmsProductAttributeCategoryExample.setOrderByClause("id desc");
        return productAttributeCategoryMapper.selectByExample(pmsProductAttributeCategoryExample);
    }

    @Override
    public List<PmsProductAttributeCategoryItem> getListWithAttr() {
        return productAttributeCategoryDao.getListWithAttr();
    }
}
